package Servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import Clases.*;
import aso.upna.cancion.CancionRemota;
import aso.upna.playlist.GenericIterator;
import aso.upna.playlist.GenericList;
import aso.upna.rmi.GetCancionesInterface;
import java.io.*;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import rmi.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;

/**
 *
 * @author Cédric
 */
public class Letras extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println(
                "<html>" +
                    "<head>" +
                        "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>" +
                        "<title>Letras</title>" +
                        "<style type=\"text/css\">"+
                            "@import url(style.css);"+
                        "</style>" +
                    "</head>" +
                    "<body>" +
                        "<div id=\"container\">" +
                            "<div id=\"header\">" +
                            "</div>" +
                            "<ul class=\"menu1\">" +
                                "<li><a href=\"./index.jsp\"><b>Inicio</b></a></li>" +
                                "<li><a href=\"ServletYouTube\"><b>Vídeos</b></a></li>" +
                                "<li class=\"current\"><a href=\"Letras\"><b>Letras</b></a></li>" +
                                "<li><a href=\"Imagenes\"><b>Imágenes</b></a></li>" +
                            "</ul>" +
                            "<div id=\"body\"></br></br>" +
                                "<div class=\"box\">" +
                                    "</br><div class=\"centrado\">"
            );
                                       /* ClientRMI almeta = new ClientRMI();
                                        Vector letras=null;
                                        try {
                                            letras=almeta.MetadatosRemoto("10.1.1.25",1085,"Letras");
                                        } catch (NamingException ex) {
                                            Logger.getLogger(Registrar.class.getName()).log(Level.SEVERE, null, ex);
                                        }
                                        
                                       // out.println("<br/><br/><div class=\"box\">");

                                            for(int i=0;i<letras.size();i++){
                                                System.out.println("recibido: "+letras.get(i)+"\n");
                                                out.println(letras.get(i)+"<br/>");
                                            }*/

            
                                            out.println("<form method=POST action=\"ProcesaLetra\">");
                                                out.println("<select name=cancion size=20>");
                                        try {
                                            GetCancionesInterface ser = (GetCancionesInterface) Naming.lookup("rmi://192.168.1.101:1084/sin_proc");
                                            GenericList users = ser.loggedUsers();
                                            GenericIterator iter = users.getIterator();
                                            while(iter.hasNext())
                                            {
                                                String usr = (String)iter.current();
                                                GenericList aux = ser.getCancionesFrom(usr);
                                                GenericIterator u_iter = aux.getIterator();
                                                while(u_iter.hasNext())
                                                {
                                                    CancionRemota c = (CancionRemota)u_iter.current();
                                                    String val = c.getMetadatos().getGrupo()+" - "+c.getMetadatos().getTitulo();
                                                    out.println("<option value=\""+val+"\">"+val);
                                                    u_iter.next();
                                                }
                                                iter.next();
                                            }
                                        } catch (NotBoundException ex) {
                                            Logger.getLogger(Letras.class.getName()).log(Level.SEVERE, null, ex);
                                        } catch (MalformedURLException ex) {
                                            Logger.getLogger(Letras.class.getName()).log(Level.SEVERE, null, ex);
                                        } catch (RemoteException ex) {
                                            Logger.getLogger(Letras.class.getName()).log(Level.SEVERE, null, ex);
                                        }
                                                    out.println("</select><br/><br/>");
                                                out.println("<input type=submit name=info value=\"Ampliar información\">");
                                            out.println("</form>");

                                        out.println("</div>");
                                        out.println("</div><br/><br/>");

            out.println(
                                //"</div></br></br>" +
                            "</div>" +
                        "</div>" +
                    "</body>" +
                "</html>"
            );
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
